package com.yuexun.beilunpatient.utils;

import android.util.Base64;
import android.util.Log;

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import okio.Buffer;

/**
 * Created by chenss on 2019/6/11.
 */
public class MyIntercepter implements Interceptor {

    private static final Charset UTF8 = Charset.forName("UTF-8");

    @Override
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        RequestBody requestBody = request.body();

        Buffer buffer = new Buffer();
        requestBody.writeTo(buffer);

        Log.i("TEST", "url ->" + request.url());
        Log.i("TEST", "原始参数 ->" + getParamContent(requestBody));


        Response response = chain.proceed(rebuildPostRequest(request));
        return response;
    }

    /**
     * 对post请求添加统一参数
     */
    private Request rebuildPostRequest(Request request) throws IOException {

        RequestBody requestBody = request.body();
        assert requestBody != null;
        RequestBody newRequestBody;
        //旧参数
        String data = getParamContent(requestBody);

        if(!data.isEmpty()){
            String[] databyte = data.split("&");
            String newdata = "";
            for(int i=0; i<databyte.length; i++){
                String[] items = databyte[i].split("=",-1);
                newdata = newdata + "@" + items[0] + "=" + "@" + items[1] + "&";
            }

            newdata = newdata.substring(1, newdata.length()-1);
            Log.i("TEST", "新参数 ->" + newdata);

            // 将普通字符创转换成application/x-www-from-urlencoded字符串
            String urlString = URLEncoder.encode(newdata, "utf-8");
            //BASE64 加密
            String encodedText = Base64.encodeToString(urlString.getBytes("utf-8"), Base64.NO_WRAP);

            newRequestBody = RequestBody.create(requestBody.contentType(), "ACTION_MESSAGE" + "=" + encodedText);
            Log.i("TEST", "加密后参数 ->" + getParamContent(newRequestBody));
        }else{
            newRequestBody = requestBody;
        }

        return request.newBuilder().method(request.method(), newRequestBody).build();
    }

    /**
     * 获取常规post请求参数
     */
    private String getParamContent(RequestBody body) throws IOException {
        Buffer buffer = new Buffer();
        body.writeTo(buffer);
        return buffer.readUtf8();
    }

    /**
     * 对get请求做统一参数处理
     */
//    private Request rebuildGetRequest(Request request) {
//        if (commonParams == null || commonParams.size() == 0) {
//            return request;
//        }
//        String url = request.url().toString();
//        int separatorIndex = url.lastIndexOf("?");
//        StringBuilder sb = new StringBuilder(url);
//        if (separatorIndex == -1) {
//            sb.append("?");
//        }
//        for (String commonParamKey : commonParams.keySet()) {
//            sb.append("&").append(commonParamKey).append("=").append(commonParams.get(commonParamKey));
//        }
//        Request.Builder requestBuilder = request.newBuilder();
//        return requestBuilder.url(sb.toString()).build();
//    }

}
